package com.ldatum.crm.action.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.ParentPackage;

import com.ldatum.crm.domain.Cart;
import com.ldatum.crm.domain.Category;
import com.ldatum.crm.domain.Medicine;
import com.ldatum.crm.domain.Supplier;
import com.ldatum.crm.service.CategoryService;
import com.ldatum.crm.service.MedicineService;
import com.ldatum.crm.service.SupplierService;
import com.ldatum.crm.serviceImpl.CategoryServiceImpl;
import com.ldatum.crm.serviceImpl.MedicineServiceImpl;
import com.ldatum.crm.serviceImpl.SupplierServiceImpl;
@ParentPackage("admin")
public class MedicineAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	private MedicineService medicineService = new MedicineServiceImpl();
	private SupplierService supplierService = new SupplierServiceImpl();
	private CategoryService categoryService = new CategoryServiceImpl();
	
	private Medicine medicine = new Medicine();
	private Category category= new Category();
	private String ids;//药品的id
	private String level;//会员卡号
	private String idCart;//已经在购物车中的药品id
	
	private int id;
	private String no;
	private String name;
	private String type;
	private String su_name;
	private double purchase_price;
	private double retail_price;
	private String introduce;
	private String prodate;
	private String exdate;
	private String remarks;
	
	
	private List<Medicine> medicineList =new ArrayList<Medicine>();
	private List<Supplier> supplierList =new ArrayList<Supplier>();
	private List<Cart> cartList =new ArrayList<Cart>();

	private List<Category> categoryList;
	
	/**
	 * 
	 * 描述 :新建药品信息
	 * @return
	 * @throws Exception
	 */
	public String add() throws Exception{
		supplierList = supplierService.list();
		categoryList = categoryService.list();
		return "addMedicine";
	}
	
	/**
	 * 保存药品信息
	 * @throws Exception3
	 */
	public void save() throws Exception{
		medicine.setExdate(exdate);
		medicine.setId(id);
		medicine.setIntro(introduce);
		medicine.setName(name);
		medicine.setNo(no);
		medicine.setProdate(prodate);
		medicine.setPurchase_price(purchase_price);
		medicine.setRemarks(remarks);
		medicine.setRetail_price(retail_price);
		medicine.setSupplierName(su_name);
		medicine.setType(type);
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("type/html");
		response.setCharacterEncoding("utf-8");
		PrintWriter out;
		out = response.getWriter();
		JSONObject json = new JSONObject();
		int result;
		if("".equals(id) || id == 0){
			result = medicineService.save(medicine);
		}else{
			result = medicineService.update(medicine);
		}
		if(result==1){
			json.accumulate("success", true);
		}else{
			json.accumulate("success", false);
		}
		out.println(json.toString());
		out.flush();
		out.close();
	}
	
	/**
	 * 
	 * 描述 :药品列表
	 * @return
	 * @throws SQLException 
	 */
	public String list() throws SQLException{
		medicineList = medicineService.list();
		return "listMedicine";
	}
	
	/**
	 * 
	 * 描述 : 
	 * @return
	 */
	public String edit(){
		try {
			medicine = medicineService.queryById(id);
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return "addMedicine";
	}
	
	/**
	 * 
	 * 描述 : <描述函数实现的功能>. <br>
	 * @return
	 * @throws IOException 
	 * @throws SQLException 
	 */
	public void deletes() throws IOException, SQLException{
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("type/html");
		response.setCharacterEncoding("utf-8");
		PrintWriter out;
		out = response.getWriter();
		JSONObject json = new JSONObject();
		if(medicineService.deletes(ids)==1){
			json.accumulate("success", true);
		}else{
			json.accumulate("success", false);
		}
		out.println(json.toString());
		out.flush();
		out.close();
	}
	
	/**
	 * 
	 * 描述 : 查询选中的的药品信息.并添加到购物车中 <br>
	 * @return
	 * @throws Exception 
	 */
	public void compute() throws Exception{
		ids = ids.substring(0, ids.length()-1);
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("type/html");
		response.setCharacterEncoding("utf-8");
		PrintWriter out;
		out = response.getWriter();
		JSONObject json = new JSONObject();
		cartList = medicineService.findAmountAndPriceByIds(ids,level,idCart);
		json.accumulate("success", true);
		json.accumulate("cart", cartList);
		json.accumulate("", "asd");
		out.println(json.toString());
		out.flush();
		out.close();
	}

	public MedicineService getMedicineService() {
		return medicineService;
	}

	public void setMedicineService(MedicineService medicineService) {
		this.medicineService = medicineService;
	}

	public Medicine getMedicine() {
		return medicine;
	}

	public void setMedicine(Medicine medicine) {
		this.medicine = medicine;
	}
	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public List<Medicine> getMedicineList() {
		return medicineList;
	}

	public void setMedicineList(List<Medicine> medicineList) {
		this.medicineList = medicineList;
	}

	public String getLevel() {
		return level;
	}

	public void setLevel(String level) {
		this.level = level;
	}

	public List<Cart> getCartList() {
		return cartList;
	}

	public void setCartList(List<Cart> cartList) {
		this.cartList = cartList;
	}

	public String getIdCart() {
		return idCart;
	}

	public void setIdCart(String idCart) {
		this.idCart = idCart;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getNo() {
		return no;
	}

	public void setNo(String no) {
		this.no = no;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getSu_name() {
		return su_name;
	}

	public void setSu_name(String su_name) {
		this.su_name = su_name;
	}
	public String getIntroduce() {
		return introduce;
	}

	public double getPurchase_price() {
		return purchase_price;
	}

	public void setPurchase_price(double purchase_price) {
		this.purchase_price = purchase_price;
	}

	public double getRetail_price() {
		return retail_price;
	}

	public void setRetail_price(double retail_price) {
		this.retail_price = retail_price;
	}

	public void setIntroduce(String introduce) {
		this.introduce = introduce;
	}

	public String getProdate() {
		return prodate;
	}

	public void setProdate(String prodate) {
		this.prodate = prodate;
	}

	public String getExdate() {
		return exdate;
	}

	public void setExdate(String exdate) {
		this.exdate = exdate;
	}

	public String getRemarks() {
		return remarks;
	}

	public void setRemarks(String remarks) {
		this.remarks = remarks;
	}

	public List<Supplier> getSupplierList() {
		return supplierList;
	}

	public void setSupplierList(List<Supplier> supplierList) {
		this.supplierList = supplierList;
	}

	public Category getCategory() {
		return category;
	}

	public void setCategory(Category category) {
		this.category = category;
	}

	public List<Category> getCategoryList() {
		return categoryList;
	}

	public void setCategoryList(List<Category> categoryList) {
		this.categoryList = categoryList;
	}
	
}
